/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cranim;

import cranim.simulation.Player;
import cranim.simulation.SimCase;
import cranim.simulation.Simulation;
import java.util.ArrayList;

/**
 *
 * @author Benedikt
 */
public class Simulator implements Constants{
    
    private Player attacker, defender, genAttacker, genDefender;
    private ArrayList<SimCase> cases;
    
    public Simulator(Player attacker, Player defender){
        this.attacker = attacker;
        this.genAttacker = attacker.clonePlayer();
        this.defender = defender;
        this.genDefender = defender.clonePlayer();
        cases = new ArrayList<>();
    }    
    
    public void run(){
        Simulation sim = new Simulation(attacker, defender, SIM_ROUNDS);
        cases = sim.run();
    }
    
    public SimCase getAverage(){
        SimCase averageAttackerCase = cases.get(0);
        SimCase averageDefenderCase = cases.get(0);
        SimCase averageCase = cases.get(0);
        int attackerHullDamgeDone = 0;
        int defenderHullDamgeDone = 0;
        for(int i = 0; i < cases.size(); i++){
            attackerHullDamgeDone += cases.get(i).getHullDamgeDone()[ATTACKER];
            defenderHullDamgeDone += cases.get(i).getHullDamgeDone()[DEFENDER];
        }
        double averageAttackerHullDamgeDone = attackerHullDamgeDone/(double)cases.size();
        double averageDefenderHullDamgeDone = defenderHullDamgeDone/(double)cases.size();
       
        for(int i = 0; i < cases.size(); i++){
            if(Math.abs(cases.get(i).getHullDamgeDone()[ATTACKER]-averageAttackerHullDamgeDone) < Math.abs(averageAttackerCase.getHullDamgeDone()[ATTACKER]-averageAttackerHullDamgeDone)){
                averageAttackerCase = cases.get(i);
            }
            if(Math.abs(cases.get(i).getHullDamgeDone()[DEFENDER]-averageDefenderHullDamgeDone) < Math.abs(averageDefenderCase.getHullDamgeDone()[DEFENDER]-averageDefenderHullDamgeDone)){
                averageDefenderCase = cases.get(i);
            }
        }
        if(!averageAttackerCase.equals(averageDefenderCase)){
            for(int i = 0; i < cases.size(); i++){
                if(Math.abs(cases.get(i).getHullDamgeDone()[ATTACKER]-averageAttackerCase.getHullDamgeDone()[ATTACKER]) < Math.abs(averageCase.getHullDamgeDone()[ATTACKER]-averageAttackerCase.getHullDamgeDone()[ATTACKER])
                && Math.abs(cases.get(i).getHullDamgeDone()[DEFENDER]-averageDefenderCase.getHullDamgeDone()[DEFENDER]) < Math.abs(averageCase.getHullDamgeDone()[DEFENDER]-averageDefenderCase.getHullDamgeDone()[ATTACKER])){
                    averageCase = cases.get(i);
                }
            }
        }
        else{
            averageCase = averageAttackerCase;
        }
        
        System.out.println("Average hull damge done fired attacker: "+averageAttackerHullDamgeDone+", defender: "+averageDefenderHullDamgeDone+" selected case with "+averageCase.getHullDamgeDone()[ATTACKER]+"/"+averageCase.getHullDamgeDone()[DEFENDER]);
        System.out.println("Average ships in the end: "+averageCase.getRound(averageCase.getRounds().size()-1).getAttacker().getFleet().getSize()+" attacker and "+averageCase.getRound(averageCase.getRounds().size()-1).getDefender().getFleet().getSize()+" defender");
        return averageCase;
    }
    
}
